/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import java.util.List;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import times.TimesDAO;
import times.persistence.entities.PriceSourceSlaughterHeader;
import times.persistence.entities.PriceSource;
import java.util.Set;
import org.zkoss.zk.ui.event.ForwardEvent;
import static org.zkoss.zk.ui.util.Clients.log;
import org.zkoss.zul.Button;
import org.zkoss.zul.Div;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Listitem;
import times.persistence.controllers.exceptions.NonexistentEntityException;
import times.persistence.entities.AnimalSpecies;
import times.persistence.entities.Category;
import times.persistence.entities.CategoryClass;
import times.persistence.entities.Foreas;
import times.persistence.entities.PriceSlaughter;
import times.persistence.entities.SlaughterProperty;
import times.persistence.entities.PropertyPriceSource;
import times.persistence.entities.PriceSlaughterLamb;
/**
 *
 * @author Fani
 */
public class PriceSourceSlaughterHeaderUI extends SelectorComposer<Component> {

    @Wire
    Datebox headerDateTxt;

    @Wire
    Textbox weekTxt;

    @Wire
    Textbox protokoloTxt;

    @Wire
    Listbox animalSpeciesLst;

    @Wire
    Listbox categoryLst;

    @Wire
    Listbox categoryClassLst;

    @Wire
    Div boo_div;

    @Wire
    Listbox priceSlaughterLst;

    @Wire
    Intbox numSlaughteredTxt;

    @Wire
    Doublebox weightTxt;

    @Wire
    Doublebox priceTxt;

    @Wire
    Doublebox correctedPriceTxt;

    @Wire
    Textbox commentsTxt;

    @Wire
    Listbox priceSourceSlaughterHeaderLst;

    @Wire
    Listbox slaughterPropertyLst;

    @Wire
    Listbox propertyPriceSourceLst;

    @Wire
    Doublebox posostoSymetoxisTxt;
    


    ListModelList<PriceSourceSlaughterHeader> priceSourceSlaughterHeaderLml;
    ListModelList<Foreas> foreasLml;
    ListModelList<Category> categoryLml;
    ListModelList<AnimalSpecies> animalSpeciesLml;
    ListModelList<CategoryClass> categoryClassLml;
    ListModelList<PriceSlaughter> priceSlaughterLml;
    ListModelList<SlaughterProperty> slaughterPropertyLml;
    ListModelList<PropertyPriceSource> propertyPriceSourceLml;
    ListModelList<PriceSlaughterLamb> priceSlaughterLambLml;

    private Boolean newPriceSourceSlaughterHeader;
    private PriceSourceSlaughterHeader priceSourceSlaughterHeader;
    private TimesDAO tdao;
    private PriceSource priceSource;
    private boolean newPriceSlaughter;
    private PriceSlaughter priceSlaughter;
    
    private SlaughterProperty slaughterProperty;
    private PriceSlaughterLamb priceSlaughterLamb;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        tdao = new TimesDAO();

        // fake login
        priceSource = tdao.findPriceSourceById(1);
        // eisagogi

        animalSpeciesLml = new ListModelList(tdao.findAnimalSpeciesEntities());
        animalSpeciesLst.setModel(animalSpeciesLml);

        newPriceSourceSlaughterHeader = true;

     
    }

    @Listen("onClick = #newBtn")
    public void onNewBtnClick() {
        newPriceSourceSlaughterHeader = true;
        priceSourceSlaughterHeader = new PriceSourceSlaughterHeader();
        Messagebox.show("Συμπληρωστε και πατηστε αποθηκευση");
        clearUI();
    }

    @Listen("onClick = #saveHeaderBtn")
    public void onSaveHeaderBtnClick() throws Exception {

        Set<AnimalSpecies> selection = animalSpeciesLml.getSelection();

        if (selection.isEmpty()) {
            Messagebox.show("Δεν έχει επιλεχθεί είδος ζώου!");
        } else {
            

            priceSourceSlaughterHeader = new PriceSourceSlaughterHeader();

            getUI();

            tdao.createPriceSourceSlaughterHeader(priceSourceSlaughterHeader);
      
            
            boo_div.setVisible(true);
            

            
            AnimalSpecies next = selection.iterator().next();
      
            List<Category> categoryList = next.getCategoryList();

            categoryLml = new ListModelList(categoryList);

            categoryLst.setModel(categoryLml);        
            
            

            List<PriceSlaughter> priceSlaughterList = priceSourceSlaughterHeader.getPriceSlaughterList();

            priceSlaughterLml = new ListModelList(priceSlaughterList);

            priceSlaughterLst.setModel(priceSlaughterLml);

            List<SlaughterProperty> slaughterPropertyList = priceSourceSlaughterHeader.getSlaughterPropertyList();

            slaughterPropertyLml = new ListModelList(slaughterPropertyList);

            slaughterPropertyLst.setModel(slaughterPropertyLml);

            propertyPriceSourceLml = new ListModelList(tdao.findPropertyPriceSourceEntities());
            propertyPriceSourceLst.setModel(propertyPriceSourceLml);
            
     //       List<PriceSlaughterLamb> priceSlaughterLambList = priceSourceSlaughterHeader.getPriceSlaughterList();

      //      priceSlaughterLambLml = new ListModelList(priceSlaughterLambList);

      //      priceSlaughterLambLst.setModel(priceSlaughterLambLml);
 
            
      //      foreasLml = new ListModelList(tdao.findForeasEntities());
       //     foreasLst.setModel(foreasLml);
        }
    }

    @Listen("onSelect = #categoryLst")
    public void onCategoryLstSelect() {

        Set<Category> catSelection = categoryLml.getSelection();
//

        Category category = catSelection.iterator().next();
//
        List<CategoryClass> categoryClassList = category.getCategoryClassList();
//
        categoryClassLml = new ListModelList(categoryClassList);
//
        categoryClassLst.setModel(categoryClassLml);
    }

    public void getUI() {

        priceSourceSlaughterHeader.setDate(headerDateTxt.getValue());
        priceSourceSlaughterHeader.setWeek(weekTxt.getValue());
        priceSourceSlaughterHeader.setProtokolo(protokoloTxt.getValue());
        priceSourceSlaughterHeader.setPriceSourceId(priceSource);

    }

    private void setUI() {
        headerDateTxt.setValue(priceSourceSlaughterHeader.getDate());
        weekTxt.setValue(priceSourceSlaughterHeader.getWeek());
        protokoloTxt.setValue(priceSourceSlaughterHeader.getProtokolo());

    }

    private void clearUI() {
        //datebox.setValue(""
        weekTxt.setValue("");
        protokoloTxt.setValue("");

    }

    @Listen("onClick = #savePrSlaughterBtn")
    public void onSavePrSlaughterBtnClick() throws Exception {
        priceSlaughter = new PriceSlaughter();
        getPSUI();
        // basi
        tdao.createPriceSlaughter(priceSlaughter);
        // entity manager
        priceSourceSlaughterHeader.getPriceSlaughterList().add(priceSlaughter);
        // ui
        priceSlaughterLml.add(priceSlaughter);

        Messagebox.show("H εγγραφή αποθηκεύτηκε");
        
        clearPSUI();

    }

    @Listen("onSelect = #priceSlaughterLst")
    public void onPriceSlaughterSelect() {
        clearPSUI();
        priceSlaughter = priceSlaughterLml.getSelection().iterator().next();
        setPSUI();

    }

    public void getPSUI() {
        priceSlaughter.setNumSlaughtered(numSlaughteredTxt.intValue());
        priceSlaughter.setWeight(weightTxt.doubleValue());
        priceSlaughter.setPrice(priceTxt.doubleValue());
        priceSlaughter.setCorrectedPrice(correctedPriceTxt.intValue());
        priceSlaughter.setCategoryClassId(categoryClassLml.getSelection().iterator().next());
        priceSlaughter.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeader);

        priceSlaughter.setComments(commentsTxt.getValue());

    }

    private void setPSUI() {
        numSlaughteredTxt.setValue(priceSlaughter.getNumSlaughtered());
        weightTxt.setValue(priceSlaughter.getWeight());
        priceTxt.setValue(priceSlaughter.getPrice());
        correctedPriceTxt.setValue(priceSlaughter.getCorrectedPrice());
        commentsTxt.setValue(priceSlaughter.getComments());
        categoryClassLml.addToSelection(priceSlaughter.getCategoryClassId());

    }

    private void clearPSUI() {
        numSlaughteredTxt.setRawValue("");
        weightTxt.setRawValue("");
        priceTxt.setRawValue("");
        correctedPriceTxt.setRawValue("");
        commentsTxt.setValue("");
    }

    @Listen("onClick = #saveSlaughterPropertyBtn")
    public void onSaveSlaughterPropertyBtnClick() throws Exception {
        slaughterProperty = new SlaughterProperty();
        getSPUI();
        // basi
        tdao.createSlaughterProperty(slaughterProperty);
        // entity manager
        priceSourceSlaughterHeader.getSlaughterPropertyList().add(slaughterProperty);
        // ui
        slaughterPropertyLml.add(slaughterProperty);

        Messagebox.show("H εγγραφή αποθηκεύτηκε");
        clearSPUI();
    }

    @Listen("onSelect = #slaughterPropertyLst")
    public void onSlaughterPropertySelect() {
        clearSPUI();
        slaughterProperty = slaughterPropertyLml.getSelection().iterator().next();
        setSPUI();

    }

    public void getSPUI() {

        slaughterProperty.setPropertyPriceSourceId(propertyPriceSourceLml.getSelection().iterator().next());
        slaughterProperty.setPosostoSymetoxis(posostoSymetoxisTxt.doubleValue());
        slaughterProperty.setPriceSourceSlaughterHeaderId(priceSourceSlaughterHeader);

    }

    private void setSPUI() {
        propertyPriceSourceLml.addToSelection(slaughterProperty.getPropertyPriceSourceId());
        posostoSymetoxisTxt.setValue(slaughterProperty.getPosostoSymetoxis());

    }

    private void clearSPUI() {
        posostoSymetoxisTxt.setRawValue("");

    }

    @Listen("onClick = #endsaveBtn")
    public void onEndsaveBtnClick() {
        Executions.sendRedirect("index.zul");
    }

    @Listen("onSlaughterDelete = #slaughterPropertyLst")
    public void onSlaughterDelete(ForwardEvent event) throws NonexistentEntityException {
 
        Button button  = (Button) event.getOrigin().getTarget();
        
        Listitem listitem = (Listitem) button.getParent().getParent(); 
        
        slaughterProperty =  listitem.getValue();
       
        
        log("deleting :" + slaughterProperty.getPosostoSymetoxis());

        // diagrafi apo listmodel
        slaughterPropertyLml.remove(slaughterProperty);

        // basi
        tdao.deleteSlaughterProperty(slaughterProperty.getId());
        
         Messagebox.show("H εγγραφή διαγράφτηκε");
       
        clearSPUI();
        
    }
    
        @Listen("onPriceSlaughterDelete = #priceSlaughterLst")
        public void onPriceSlaughterDelete(ForwardEvent event) throws NonexistentEntityException {
 
        Button button  = (Button) event.getOrigin().getTarget();
        
        Listitem listitem = (Listitem) button.getParent().getParent(); 
        
        priceSlaughter =  listitem.getValue();
       
        
        // diagrafi apo listmodel
         priceSlaughterLml.remove( priceSlaughter);

        // basi
        tdao.deletePriceSlaughter(priceSlaughter.getId());
       
        Messagebox.show("H εγγραφή διαγράφτηκε");
        
        clearPSUI();
    }

 
        
        
        
    }

